package cn.itcast.bos.web.action.system;

import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;

import cn.itcast.bos.domain.system.Menu;
import cn.itcast.bos.domain.system.User;
import cn.itcast.bos.service.system.MenuService;
import cn.itcast.bos.web.action.common.BaseAction;

@SuppressWarnings("serial")
@Namespace("/")
@ParentPackage("json-default")
@Controller
@Scope("prototype")
public class MenuAction extends BaseAction<Menu>{
	
	@Autowired
	private MenuService menuService;
	
	@Action(value="menu_findAll",results={@Result(name="success",type="json")})
	public String menu_findAll(){
		List<Menu> menus = menuService.findAll();
		ActionContext.getContext().getValueStack().push(menus);
		return SUCCESS;
	}
	
	/**
	 * 手动添加树形菜单
	 * @return
	 */
	@Action(value="menu_add",results={@Result(name="success",type="redirect",location="pages/system/menu.html")})
	public String menu_add(){
		menuService.menuAdd(model);
		return SUCCESS;
	}
	
	//页面初始化根据用户加载树形菜单
	@Action(value="menu_usermenu",results={@Result(name="success",type="json")})
	public String userMenu(){
		//根据用户登录的信息，shiro的控制中心securityMenager查询用户的职能
		Subject subject = SecurityUtils.getSubject();
		User user = (User) subject.getPrincipal();
		List<Menu> menus = menuService.findByUser(user);
		if(!menus.isEmpty()){
			ActionContext.getContext().getValueStack().push(menus);
		}
		return SUCCESS;
	}

}
